package com.shanglida.android;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shanglida.R;
import com.shanglida.base.BaseActivity;
import com.shanglida.base.UserData;
import com.shanglida.base.UserService;
import com.shanglida.base.BaseActivity.IntentSpan;
import com.shanglida.main.TabSet;
import com.shanglida.main.find.WebActivity;

/**
 * @author liu
 * @deprecated 注册界面
 * @version 1.0
 * @date 2014-02-25 修改
 * @copyright 尚礼达信息科技有限公司
 */
public class RegisActivity extends BaseActivity {

	private static final String URL = "http://m.51m51m.cn/user/reginfo.html";
	// 清除数据
	private ImageView ReClear;
	// 服务条款
	private TextView items;
	// 注册按钮
	private Button mRegis;
	// 依次为 用户名 用户密码 确认用户密码 用户手机号码
	private EditText mName, mPass, mPase, mPhone;

	/*
	 * initView 初始化 initData 监听事件
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_activity);
		initView();
		initData();
		addLink();
	}

	/**
	 * initData 设置监听器
	 */
	protected void initData() {
		ReClear.setOnClickListener(this);
		mRegis.setOnClickListener(this);
	}

	/**
	 * initView 初始化ID
	 */
	protected void initView() {
		ReClear = (ImageView) findViewById(R.id.rfusetheitem);
		items = (TextView) findViewById(R.id.shanglida_terms);
		mRegis = (Button) findViewById(R.id.regbutton);
		mName = (EditText) findViewById(R.id.unregiter_name);
		mPass = (EditText) findViewById(R.id.check_password);
		mPase = (EditText) findViewById(R.id.check_password_again);
		mPhone = (EditText) findViewById(R.id.check_phonumber);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.regbutton:
			try {
				RegisterService();
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case R.id.rfusetheitem:
			ReClear();
			break;

		case R.id.shanglida_terms:
			OpenLink(URL);
			break;
		default:
			break;
		}

	}

	private void OpenLink(String URL) {
		Intent Mweb = new Intent();
		Mweb.putExtra("URL", URL);
		Mweb.setClass(RegisActivity.this, WebActivity.class);
		startActivityForResult(Mweb, RESULT_OK);
	}

	/**
	 * ReClear 重置注册数据
	 */
	private void ReClear() {
		mName.setText("");
		mPass.setText("");
		mPase.setText("");
		mPhone.setText("");
	}

	private void RegisterService() {
		// 依次为 用户名 用户密码 确认用户密码 用户手机号码
		String NAME = mName.getText().toString().trim();
		String PASS = mPass.getText().toString().trim();
		String PASE = mPase.getText().toString().trim();
		String PHONE = mPhone.getText().toString().trim();
		UserService use = new UserService(RegisActivity.this);
		UserData user = new UserData();
		user.setUsername(NAME);
		user.setNumbile(PASS);
		user.setPassword(PASE);
		user.setPaesword(PHONE);
		use.register(user);
		Toast.makeText(RegisActivity.this, "注册成功", Toast.LENGTH_LONG).show();
		Intent Ulog = new Intent();
		// Ulog.putExtra("USERNAME", NAME);
		// Ulog.putExtra("PASSWORD", PASS);
		Ulog.setClass(RegisActivity.this, LoginActivity.class);
		startActivity(Ulog);

	}

	/**
	 * addLink 类似链接
	 */
	private void addLink() {
		String msg = getString(R.string.items_html);
		SpannableString msb = new SpannableString(msg);
		msb.setSpan(new IntentSpan(new OnClickListener() {
			public void onClick(View view) {
				// 跳转到找回密码界面
				// startActivity(FindPasswordActivity.class);
				// forgetwd();
			}
		}), 0, msb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		msb.setSpan(
				new ForegroundColorSpan(getResources().getColor(R.color.white)),
				0, msb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		// 设置文本内容
		items.setText(msb);
		items.setMovementMethod(LinkMovementMethod.getInstance());
	}
}
